import settings
from httpx import post, get


class BaseApi:
    # 从 django settings 获取蓝鲸应用信息（app_code=settings.APP_CODE，app_secret=settings.SECRET_KEY）
    # 参数 request 为 django Request 对象，其中包含用户信息
    def __init__(self):
        pass
        # self.client = get_client_by_request(request)
        # # 设置是否访问第三方系统的测试环境，默认值为False，访问其正式环境
        # self.client.set_use_test_env(True)
        #
        # # 设置访问组件API的超时时间，单位秒
        # self.client.set_timeout(10)
        # self.secret = {
        #     "bk_app_code": settings.APP_CODE,
        #     "bk_app_secret": settings.SECRET_KEY,
        #     "bk_username": request.user.username,
        #     "bk_token": settings.SECRET_KEY,
        # }
        # bk_token, bk_username
        # self.client = ComponentClient(app_code=settings.APP_CODE, app_secret=settings.SECRET_KEY)
        # self.client = Client(app_code=settings.APP_CODE, app_secret=settings.SECRET_KEY)

    def requests(self, method: str, url: str, data: dict = {}) -> dict:
        try:
            req_method = method.lower()
            if req_method == 'get':
                req = get(url=url, verify=False)
            elif req_method == 'post':
                data["bk_username"] = settings.BK_USERNAME  # 可以不需要
                data["bk_token"] = settings.BK_TOKEN
                data["bk_app_code"] = settings.BK_APP_CODE
                data["bk_app_secret"] = settings.BK_APP_SECRET
                req = post(url="{0}{1}/".format(settings.CMDB_API_URL, url), json=data, verify=False)
                return req.json()
        except Exception as e:
            return {"result": False, "code": 0, "data": [], "message": e, "permission": e}
